package com.eol.common.utils;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import com.eol.common.exception.BaseException;

import java.math.BigDecimal;

/**
 * Created with IntelliJ IDEA.
 * User: liulinhui
 * Date: 2020/3/31
 * Time: 5:57 PM
 * Description: PayUtils
 */
@Slf4j
public class PayUtils {

    /**
     * 计算退款手续费
     *
     * @param refundMoney 退款金额
     * @return 退款扣除收学费
     */
    public static BigDecimal calFee(BigDecimal refundMoney) {
        return refundMoney.multiply(new BigDecimal("0.994")).setScale(2, BigDecimal.ROUND_HALF_DOWN);
    }

    /**
     * 判断支付宝退款是否成功
     *
     * @param res res
     */
    public static void checkAliRefundSuccess(String res) {
        JSONObject jsonObject = (JSONObject) JSONObject.parse(res);
        JSONObject alipay_trade_refund_response = jsonObject.getJSONObject("alipay_trade_refund_response");
        if (null == alipay_trade_refund_response) {
            log.error("支付宝退款失败:" + res);
            throw new BaseException("支付宝退款失败");
        }
        String code = alipay_trade_refund_response.getString("code");
        if (!"10000".equalsIgnoreCase(code)) {
            log.error("支付宝退款失败:" + res);
            throw new BaseException("支付宝退款失败:" + alipay_trade_refund_response.getString("sub_msg"));
        }

    }
}
